#coding:utf-8

import sys, os
import datetime, time
import sqlite3 as sql
import stock
import text
import database
from dttime import now, series
import datamethod as dm

class RecordInfo(database.BaseInfo):
    general_fields = ('id', 'begin', 'end', 'busstype', 
                      'bussdata', 'repeat', 'patience', 'rest')
    general_table = 'general'
    basic_fields = ('tel', 'testbegin', 'testend')
    basicinfo_table = 'testinfo'
    data_fields = ('time', 'busstype', 'bussdata')
    def __init__(self, connection):
        self.setConnection(connection)
        self._current = None
        
    def initTable(self, tel):
        self.create(self.general_table, *self.general_fields)
        self.create(self.basicinfo_table, *self.basic_fields)
        self._setReportName(tel)
        
    def _setReportName(self, vehilce):
        if self.select(self.basicinfo_table, ''):
            self.update(self.basicinfo_table, '', tel=vehilce)
        else:
            self.insert(self.basicinfo_table, tel=vehilce)
            
    def recordTestStarted(self):
        self.update(self.basicinfo_table, '', testbegin=now())
            
    def recordTestClosed(self):
        self.closeRecord()
        self.update(self.basicinfo_table, '', testend=now())
        self.getConnection().close()
        
    def recordNewTestCase(self, **kw):
        self.closeRecord()
        self._current = 'id%s'%series()
        self.insert(self.general_table, id=self._current, begin = now(), **kw)
        self.create(self._current, *self.data_fields)
        return self._current
            
    def closeRecord(self):
        if self._current:
            self.update(self.general_table, end=now(),
                        where='id="%s"'%self._current)
            
    def insertData(self, busstype, bussdata):
        self.insert(self._current, time = now(), 
                    busstype = dm.data2str(busstype), 
                    bussdata = dm.data2str(bussdata))
        
    def getTestInfo(self):
        self.select(self.basicinfo_table, '', *self.basic_fields)
        results = self.getResult()
        if results:
            return results[0]
        
    def getTestTime(self):
        self.select(self.basicinfo_table, '', 'testbegin', 'testend')
        results = self.getResult()
        if results:
            return results[0]
        
    def getTestCaseData(self, id):
        self.select(self.general_table, 'id="%s"'%id, 'busstype', 'bussdata')
        results = self.getResult()
        if results:
            return results[0]
        
    def getTestCaseTime(self, id):
        self.select(self.general_table, 'id="%s"'%id, 'begin', 'time')
        results = self.getResult()
        if results:
            return results[0]
        
    def getTestCaseOption(self, id):
        self.select(self.general_table, 'id="%s"'%id, 'repeat', 'patience', 'rest')
        results = self.getResult()
        if results:
            return results[0]
        
    def getTestCaseInfo(self, id):
        self.select(self.general_table, 'id="%s"'%id, *self.general_fields)
        results = self.getResult()
        if results:
            return results[0]
    
    def getAllTestCaseInfo(self):
        self.select(self.general_table, '', *self.general_fields)
        return self.getResult()
    
    def getRecordData(self, id):
        self.select(str(id), '', *self.data_fields)
        return self.wrapper()
    
    def getSomeRecordData(self, id, busstype):
        if instance(busstype, list):
            busstype = dm.data2str(busstype)
        self.select(str(id), 'busstype="%s"'%busstype, 'time', 'bussdata')
        return self.wrapper()
    

class RecordConnection(database.Dbapi):
    def __init__(self, filename):
        self.filename = filename
        database.Dbapi.__init__(self, filename)
    
    def __str__(self):
        return "<record database connection:%s>"%self.filename
    
    def __repr__(self):
        return self.__str__()

    

if __name__ == '__main__':
    r = RecordConnection("test.db")
    while 1:
        s = raw_input(">>")
        if s == 'q': break
        x= r.execSql(s)


